package com.kuang.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.kuang.pojo.User;
import com.kuang.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@Slf4j
public class UserController {
    
    @RequestMapping(value = "/json1", produces = "application/json;charset=utf-8")
    @ResponseBody
    // Content-Type: text/html;charset=ISO-8859-1
    public String json1() throws JsonProcessingException {
        //创建一个jackson的对象映射器，用来解析数据
        ObjectMapper mapper = new ObjectMapper();
        //创建一个对象
        User user = new User("秦疆1号", 3, "男");
        
        String s = mapper.writeValueAsString(user);
        
        log.info("输出json字符串 = {}", s);
        
        System.out.println("s = " + s);
        return s;
    }
    
    @RequestMapping(value = "/json2")
    @ResponseBody
    // Content-Type: text/html;charset=ISO-8859-1
    public String json2() throws JsonProcessingException {
        //创建一个jackson的对象映射器，用来解析数据
        ObjectMapper mapper = new ObjectMapper();
        //创建一个对象
        User user = new User("秦疆1号", 3, "男");
        
        String s = mapper.writeValueAsString(user);
        
        log.info("输出json字符串 = {}", s);
        
        System.out.println("s = " + s);
        return s;
    }
    
    @RequestMapping(value = "/json3")
    @ResponseBody
    public String json3() throws JsonProcessingException {
        //创建一个jackson的对象映射器，用来解析数据
        ObjectMapper mapper = new ObjectMapper();
        //创建一个对象
        User user = new User("秦疆1号", 3, "男");
        
        String s = mapper.writeValueAsString(user);
        
        log.info("输出json字符串 = {}", s);
        
        System.out.println("s = " + s);
        return s;
    }
    
    @RequestMapping("/json4")
    @ResponseBody
    public String json4() throws JsonProcessingException {
        
        //创建一个jackson的对象映射器，用来解析数据
        ObjectMapper mapper = new ObjectMapper();
        //创建一个对象
        User user1 = new User("秦疆1号", 3, "男");
        User user2 = new User("秦疆2号", 3, "男");
        User user3 = new User("秦疆3号", 3, "男");
        User user4 = new User("秦疆4号", 3, "男");
        List<User> list = new ArrayList<User>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        
        //将我们的对象解析成为json格式
        String str = mapper.writeValueAsString(list);
        return str;
    }
    
    @RequestMapping("/json6")
    public String json6() throws JsonProcessingException {
        
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        mapper.setDateFormat(simpleDateFormat);
        
        //创建时间一个对象，java.util.Date
        Date date = new Date();
        
        List<Date> list = Arrays.asList(new Date(), new Date());
        //将我们的对象解析成为json格式
        String str = mapper.writeValueAsString(list);
        return str;
    }
    
    @RequestMapping("/json7")
    public String json7() throws JsonProcessingException {
        
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        mapper.setDateFormat(simpleDateFormat);
        
        //创建时间一个对象，java.util.Date
        Date date = new Date();
        
        //将我们的对象解析成为json格式
        String str = mapper.writeValueAsString(date);
        return str;
    }
    
    @RequestMapping("/json8")
    public String json8() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS, false);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm::ss"));
        
        User user = new User("阿勇", 3, "男");
        user.setBirth(new Date());
        
        String json = JsonUtils.getJson(user);
        log.info("user JSON = {}", json);
        return mapper.writeValueAsString(user);
    }
    
    
}
